package Controller1;

import java.awt.Insets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.plaf.synth.SynthSeparatorUI;

import com.sun.org.apache.bcel.internal.generic.NEW;

import Dao.goodsDao;
import Dao.orderDetailDao;
import Dao.paymentDao;
import Dao.sellorderDao;
import Model1.goods;
import Model1.orderDetail;
import Model1.sellorder;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.geometry.Side;
import javafx.scene.Group;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.PieChart;
import javafx.scene.chart.PieChart.Data;
import javafx.scene.chart.ScatterChart;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.control.SplitPane;
import javafx.scene.image.Image;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;
import Model1.orderDetail;
public class chartController {
	

	@FXML
	public void start(Stage primaryStage) {
		List<orderDetail> listO = orderDetailDao.findAll();//
		List<goods> listP = goodsDao.findAll();
		List<sellorder> listOrders = sellorderDao.findAll();
		List<Map<String, Integer>> list = new ArrayList<>();
		
		String[] PN = new String[listP.size()];
		String[] monthname = {"1月","2月","3月","4月","5月","6月","7月","8月","9月","10月","11月","12月"};
		String[] dayname = {"星期一","星期二","星期三","星期四","星期五","星期六","星期天"};
		double[][] listMoney = new double[listP.size()][12];
		
		for(int i=0;i<listP.size();i++) {
			PN[i] = listP.get(i).getName();
			Map<String, Integer> map = new HashMap<String, Integer>();
			map.put(listP.get(i).getName(),0);
			list.add(map);
		}
		
		for(int i=0;i<listO.size();i++) {
			for(int j=0;j<listP.size();j++) {
				if(listO.get(i).getId()==listP.get(j).getId()) {
					int k = list.get(j).get(listP.get(j).getName())+listO.get(i).getSales();
					list.get(j).put(listP.get(j).getName(),k);
					String mon = sellorderDao.SearchById(listO.get(i).getOrderId()).getSelldate().split("-")[1];
					listMoney[j][Integer.parseInt(mon)-1]=listO.get(i).getPrice()+listMoney[j][Integer.parseInt(mon)-1];
				}
			}
		}
		
		BarChart<String, Number> barChart = null;
		BorderPane bp = new BorderPane();
		// Defining the axes
		CategoryAxis xAxis = new CategoryAxis();
		xAxis.setCategories(
				FXCollections.<String>observableArrayList(Arrays.asList(PN)));

		NumberAxis yAxis = new NumberAxis();
		yAxis.setLabel("销售量");

		// Creating the Bar chart
		barChart = new BarChart<>(xAxis, yAxis);
		barChart.setTitle("今日销售数量");

		// Prepare XYChart.Series objects by setting data
		XYChart.Series<String, Number> today = new XYChart.Series<>();
		today.setName("产品数量");
		for(int i=0;i<list.size();i++) {
			today.getData().add(new XYChart.Data<>(PN[i], list.get(i).get(PN[i])));
		}

		// Setting the data to bar chart
		barChart.getData().addAll(today);
		// Creating a Group object
		Group todayData= new Group(barChart);
		
		
		
		
		BarChart monthbar = null;
		CategoryAxis xmonth = new CategoryAxis();
		xmonth.setCategories(
				FXCollections.<String>observableArrayList(Arrays.asList(PN)));  /////
		xmonth.setLabel("产品");

		NumberAxis ymonth = new NumberAxis();
		ymonth.setLabel("销售量");

		// Creating the Bar chart
		monthbar = new BarChart<>(xmonth, ymonth);
		monthbar.setTitle("本月销售数量");
		// Prepare XYChart.Series objects by setting data
		XYChart.Series<String, Number> month = new XYChart.Series<>();
		month.setName("产品数量");
		for(int i=0;i<list.size();i++) {
			month.getData().add(new XYChart.Data<>(PN[i], list.get(i).get(PN[i])));	/////
		}

		// Setting the data to bar chart
		monthbar.getData().addAll(month);
		// Creating a Group object
		Group monthData= new Group(monthbar);
		
		final CategoryAxis xAxis1 = new CategoryAxis();
        final NumberAxis yAxis1 = new NumberAxis();
        xAxis1.setLabel("Month");
        final LineChart<String,Number> lineChart = 
                new LineChart<String,Number>(xAxis1,yAxis1);
       
        lineChart.setTitle("本年各月各产品销售额折线图");
                          
        List<Series<String, Number>> series = new ArrayList<>();
        for(int i=0;i<listP.size();i++) {
        	Series s = new Series();
        	s.setName(PN[i]);
        	series.add(s);
        	for(int j=0;j<12;j++) {
        		series.get(i).getData().add(new XYChart.Data(monthname[j], listMoney[i][j]));
        	}
        }
        lineChart.getData().addAll(series);
		
        final CategoryAxis xAxis2 = new CategoryAxis();
        final NumberAxis yAxis2 = new NumberAxis();
        xAxis2.setLabel("week");
        final LineChart<String,Number> lineChart2 = 
                new LineChart<String,Number>(xAxis2,yAxis2);
        lineChart2.setTitle("本周销售额");
        List<Series<String, Number>> series2 = new ArrayList<>();
       
        Series s = new Series();
    	s.setName("销售额");
    	series2.add(s);
    	
		
		
        for(int i=0;i<7;i++) {
        		int j=7-i-1 ;
        		
        		series2.get(0).getData().add(new XYChart.Data(getDay(j), paymentDao.SearchbyTime(getDay(j)+"%")));
        	
        }
        lineChart2.getData().addAll(series2);
        
        PieChart pieChart = new PieChart();
        pieChart.setData(getChartData());
        pieChart.setTitle("产品总销售量占比");
        pieChart.setLegendSide(Side.LEFT);
        pieChart.setClockwise(false);
        pieChart.setLabelsVisible(false);

        
        
		//网格布局
		GridPane gridPane= new GridPane();
		
		gridPane.add(barChart, 0, 0);
		gridPane.add(pieChart , 0,1);
		gridPane.add(monthbar,1,0);
		gridPane.add(lineChart2, 1,1);
		
		//设置单独组件的上下左右的间e距
		/*GridPane.setMargin(nam, new Insets(0,0,0,115));*/
		
		gridPane.setAlignment(Pos.CENTER);
		//设置垂直间距
		gridPane.setVgap(100);
		//设置水平间距
		gridPane.setHgap(100);
 
		/*Scene scene = new Scene(gridPane);*/

		Scene scene = new Scene(gridPane);
		primaryStage.setHeight(1050);
		primaryStage.setWidth(1300);
		primaryStage.setTitle("报表");
		primaryStage.setScene(scene);
		primaryStage.show();
	}
	
	public String getDay(int i) {
		
		Calendar calendar=Calendar.getInstance();
		int year = calendar.get(Calendar.YEAR);
		int day = calendar.get(Calendar.DAY_OF_MONTH);
		int month = calendar.get(Calendar.MONTH)+1;
		int weekday = calendar.get(Calendar.DAY_OF_WEEK);
		String date=""+year+"-"+month+"-"+day;
		if(i==0) 
			return date;
		else {
			if(day-i<=0&&month-1<=0) {
				year--;
				month=12;
				int flag=i-day;
				day=31-flag;
				date=""+year+"-"+month+"-"+day;
				return date;
			}
			else if(day-i<=0&&month>1) {
				month--;
				int flag=i-day;
				day=31-flag;
				date=""+year+"-"+month+"-"+day;
				return date;
			}
			else if(day>i) {
				day-=i;
				date=""+year+"-"+month+"-"+day;
				return date;
			}
		}
		return "no";
	}
	
	private ObservableList<Data> getChartData() {
        ObservableList<Data> answer = FXCollections.observableArrayList();
        answer.addAll(new PieChart.Data("咖啡", orderDetailDao.SearchByName("咖啡")),
        		new PieChart.Data("沙拉", orderDetailDao.SearchByName("沙拉")),new PieChart.Data("牛扒", orderDetailDao.SearchByName("牛扒")),new PieChart.Data("蛋糕", orderDetailDao.SearchByName("蛋糕")));
        return answer;
    }

}
